//
//  JSTPublishInfoView.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/16.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTPublishInfoView.h"
#import "JSTPublishBar.h"

@interface JSTPublishInfoView ()<PublishBarDelegate>

@property (nonatomic, strong) JSTPublishBar *publishBar;

@end

@implementation JSTPublishInfoView

- (id)initWithFrame:(CGRect)frame andOwner:(id)owner
{
    if (self = [super initWithFrame:frame andOwner:owner])
    {
        
    }
    return self;
}

- (void)setContentMode:(UIViewContentMode)contentMode
{
    
}

- (void)createContent
{
    [self addSubview:self.publishTextView];
    
    [self addSubview:self.scrollView];
    
    if (!_dynamicId)
    {
        [self addSubview:self.publishBar];
    }
}

#pragma mark - 控件实例化
- (UITextView *)publishTextView
{
    if (!_publishTextView) {
        _publishTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, kScreenHeight - 64)];
        _publishTextView.returnKeyType = UIReturnKeyDone;
        _publishTextView.font = kSystemFont(kCurrentScreenWidth(12));
        _publishTextView.textColor = [UIColor blackColor];
        _publishTextView.delegate = self.owner;
        [_publishTextView addSubview:self.placeLabel];
        
        if (_userNick) {
            self.placeLabel.text = [NSString stringWithFormat:@"回复 %@", _userNick];
        } else {
            self.placeLabel.text = @"分享新鲜事...";
        }
    }
    return _publishTextView;
}

- (UILabel *)placeLabel
{
    if (!_placeLabel) {
        _placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kCurrentScreenWidth(7), 0, _publishTextView.frame.size.width - kCurrentScreenWidth(7), 35)];
        _placeLabel.font = kSystemFont(kCurrentScreenWidth(12));
        _placeLabel.textColor = [UIColor lightGrayColor];
    }
    return _placeLabel;
}

- (JSTPublishBar *)publishBar
{
    if (!_publishBar) {
        _publishBar = [[JSTPublishBar alloc] initWithFrame:CGRectMake(0, kScreenHeight - kCurrentScreenWidth(44) - 64, kScreenWidth, kCurrentScreenWidth(44)) andView:self.publishTextView];
        _publishBar.delegate = self;
    }
    return _publishBar;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        CGSize sizefit = [self.publishTextView sizeThatFits:CGSizeMake(kScreenWidth - 20, MAXFLOAT)];
        CGFloat height = (kScreenWidth - 2 * kCurrentScreenWidth(15) - 2*kCurrentScreenWidth(10)) / 3 + kCurrentScreenWidth(10);
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, sizefit.height + kCurrentScreenWidth(20), kScreenWidth, height)];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth - 3 * 10, 1)];
        label.backgroundColor = [UIColor colorWithRed:233/255.f green:183/255.f blue:151/255.f alpha:1.f];
        
        [_scrollView addSubview:label];
    }
    return _scrollView;
}

#pragma mark - 拿到照片赋值
- (void)setImageArr:(NSArray *)imageArr
{
    _imageArr = imageArr;
    
    for (UIView *obj in self.scrollView.subviews) {
        if ([obj isKindOfClass:[UIImageView class]]) {
            [obj removeFromSuperview];
        }
    }
    
    CGFloat width = (kScreenWidth - 2 * kCurrentScreenWidth(15) - 2 * kCurrentScreenWidth(10))/3;
    CGFloat height = width;
    [imageArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIImage *image = (UIImage *)obj;
        UIImageView *imageview=[[UIImageView alloc] initWithFrame:CGRectMake(kCurrentScreenWidth(15) + idx % 3 * (width + kCurrentScreenWidth(10)), kCurrentScreenWidth(10), width, height)];
        imageview.userInteractionEnabled = YES;
        imageview.contentMode = UIViewContentModeScaleAspectFill;
        imageview.clipsToBounds = YES;
        UITapGestureRecognizer *bigTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigTapGes:)];
        [imageview addGestureRecognizer:bigTapGes];
        imageview.tag = idx+1;
        imageview.image = image;
        [self.scrollView addSubview:imageview];
    }];
}

- (void)bigTapGes:(UITapGestureRecognizer *)tapGes
{
    if (_delegate&&[_delegate respondsToSelector:@selector(zoomingPicture:)]) {
        [_delegate zoomingPicture:tapGes.view.tag];
    }
}

- (void)deleImagView:(UITapGestureRecognizer *)tapGes
{
    UIView *view = tapGes.view;
    CGFloat width = (kScreenWidth - 2 * kCurrentScreenWidth(15) - 2 * kCurrentScreenWidth(10)) / 3;
    UIImageView *imgView =(UIImageView *)[view superview];
    [imgView removeFromSuperview];
    for (NSInteger i = view.tag+1; i <= _imageArr.count; i++) {
        [UIView animateWithDuration:0.3 animations:^{
            UIImageView *subImgView = (UIImageView *)[_scrollView viewWithTag:i];
            UIImageView *iView = subImgView.subviews[0];
            iView.tag = i-1;
            subImgView.tag = i-1;
            CGRect frame = subImgView.frame;
            frame.origin.x = frame.origin.x - kCurrentScreenWidth(10) - width;
            subImgView.frame = frame;
        }];
    }
    
    if (_delegate&&[_delegate respondsToSelector:@selector(deleteSelectPicture:)]) {
        [_delegate deleteSelectPicture:view.tag];
    }
}

#pragma mark - publishDelegate
- (void)publishBarWithBut:(UIButton *)but
{
    if (but.tag > 0) { // 添加表情
        NSString *faceName = [[NSString alloc] init];
        NSString *expressstring = [NSString stringWithFormat:@"e_%zd.png",but.tag];
        NSString *plistStr = [[NSBundle mainBundle] pathForResource:@"Emoji_icon.plist" ofType:nil];
        NSDictionary *plistDic = [[NSDictionary alloc] initWithContentsOfFile:plistStr];
        
        for (int j = 0; j<=[[plistDic allKeys]count]-1; j++) {
            if ([[plistDic objectForKey:[[plistDic allKeys]objectAtIndex:j]]
                 isEqualToString:expressstring]) {
                faceName = [[plistDic allKeys] objectAtIndex:j];
            }
        }
        
        self.publishTextView.text = [self.publishTextView.text stringByAppendingString:faceName];
        self.placeLabel.hidden = YES;
        
    } else { //删除表情
        
        if (self.publishTextView.text.length > 0) {
            if ([self.publishTextView.text hasSuffix:@"]"]) {
                NSMutableString *newString=[NSMutableString stringWithString:self.publishTextView.text];
                
                // 正则表达式
                NSString *patter = @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";
                NSError *error = nil;
                NSRegularExpression *regularExpression=[NSRegularExpression regularExpressionWithPattern:patter options:NSRegularExpressionCaseInsensitive error:&error];
                
                // 匹配文字
                NSArray *resultArray=[regularExpression matchesInString:newString options:0 range:NSMakeRange(0, newString.length)];
                NSTextCheckingResult *match=resultArray.lastObject;
                
                [newString deleteCharactersInRange:match.range];
                self.publishTextView.text = newString;
            } else {
                NSString *newStr = self.publishTextView.text;
                self.publishTextView.text = [newStr substringToIndex:newStr.length-1];
            }
        } else {
            self.placeLabel.hidden = NO;
        }
    }
    
    CGSize sizefit = [self.publishTextView sizeThatFits:CGSizeMake(kScreenWidth - 20, MAXFLOAT)];
    
    [UIView animateWithDuration:0.25 animations:^ {
        CGRect frame = self.scrollView.frame;
        frame.origin.y = sizefit.height + kCurrentScreenWidth(20);
        self.scrollView.frame = frame;
    }];
}

- (void)publishBarToStrangerWithBut:(UIButton *)but
{
    if (_delegate&&[_delegate respondsToSelector:@selector(PublishInfoViewToStrangerWithBut:)]) {
        [_delegate PublishInfoViewToStrangerWithBut:but];
    }
}

- (void)getPublishBarToPickerWithTag:(NSInteger)tag
{
    if (_delegate&&[_delegate respondsToSelector:@selector(getPublishInfoViewWithTag:)]) {
        [_delegate getPublishInfoViewWithTag:tag];
    }
}

@end
